/* Kanvas Paint and Drawing Tool.
 * Copyright (C) 2011 Darryl Wallace
 * wallacdj@gmail.com / darryl@wallynet.ca
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*!
  @file BrushTool.cpp
  @author Darryl Wallace
  @date 2011
  @brief This file defines the brush tool.
  */

#include "BrushTool.h"


BrushTool::BrushTool()
    : _parent(NULL)
{
    _p.setWidthF(3.0);
}

BrushTool::~BrushTool()
{

}

void BrushTool::setLayerItem(LayerItem *layer)
{
    setParentItem((QGraphicsItem *)layer );
    _parent=layer;
}

void BrushTool::setPrimaryColor(const QColor &p)
{
    _primary=p;
    _p.setColor(_primary);
}

void BrushTool::setSecondaryColor(const QColor &s)
{
    _secondary=s;
}

void BrushTool::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{
    painter->setPen(_p);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->drawPath(_path);
}

void BrushTool::paintOnImage(QPainter *painter)
{
    painter->setPen(_p);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->drawPath(_path);
}

void BrushTool::handleMousePressEvent(const QGraphicsSceneMouseEvent *e)
{
    _path=QPainterPath();


    _initial=e->scenePos();
    _last=e->scenePos();
    _current=e->scenePos();
    _path.moveTo(_current);
    _path.lineTo(_current);
    _boundingRect=QRectF(_current.x(), _current.y(),_p.widthF(),_p.widthF());
}

void BrushTool::handleMouseMoveEvent(const QGraphicsSceneMouseEvent *e)
{
    _last=_current;
    _current=e->scenePos();
    _path.lineTo(_current);
    _boundingRect=_path.boundingRect();
    qreal halfpw = _p.widthF()/2.0;
    _boundingRect.adjust(-halfpw, -halfpw, halfpw, halfpw);
}

void BrushTool::handleMouseReleaseEvent(const QGraphicsSceneMouseEvent *e)
{
    // Set the last position.  Not currently used.
    _last=e->scenePos();
}

QIcon BrushTool::toolIcon() const
{
    QIcon icon(":/KanvasStandardPlugin/icons/paintbrush.png");
    return icon;
}

void BrushTool::setPen(const QPen &pen)
{
    _p=pen;
}
